<?php
/**
* Calendar : Starts the calendar
*/

class Calendar extends DBC{
	/**
	* @var string Y-m-d H:i:s
	*/
	private $_startDate;
	/**
	* @var int current month
	*/
	private $_m;
	/**
	* @var int current year
	*/
	private $_y;
	/**
	* @var int total of days in current month
	*/
	private $_totalDays;
	/**
	* @var int first day of the month
	*/
	private $_startDay;
	
	/**
	* Initialize the Calendar
	* @param object $dbo DB Object
	* @param string start Date
	* @return void
	*/
	
	public function __construct($dbo=NULL, $startDate=NULL){
		parent::__construct($dbo);
		
		/* Use current date if there's no startDate */
		if(isset($startDate)){
			$this->_startDate = $startDate;
		}else{
			$this->_startDate = date('Y-m-d H:i:s');
		}
		
		$timestamp = strtotime($this->_startDate);
		$this->_m = date('m', $timestamp);
        	$this->_y = date('Y', $timestamp);
		$this->_totalDays = cal_days_in_month(CAL_GREGORIAN, $this->_m, $this->_y);
		$ts = mktime(0, 0, 0, $this->_m, 1, $this->_y);
		$this->_startDay = date('w', $ts);
	}
	
	/**
	* Sets the title of the calendar
	* @return string HTML calendar title 
	*/
	public function setTitle(){
		$title = sprintf('<div class="monthYear"><span><a href="#" class="prevM"><</a></span>%s<span class="alignRight">%s<a href="#" class="nextM">></a></span></div>',date('F', strtotime($this->_startDate)),date('Y', strtotime($this->_startDate)));
		return $title;
	}
	
	/**
	* Show the calendar
	* @return string HTML calendar table 
	*/
	
	public function show(){
		/* Implement code to generate the calendar table here */
		$html = "<table>\n\t<thead>\n\t<tr>\n\t\t<th>Su</th>\n\t\t<th>Mo</th>\n\t\t<th>Tu</th>\n\t\t<th>We</th>\n\t\t<th>Th</th>\n\t\t<th>Fr</th>\n\t\t<th>Sa</th></tr></thead><tbody><tr  class=\"weeks\">";
		/* Current day data */
		$cDay=date('j');
		$cMonth=date('m');
		$cYear=date('Y'); 
		$iDay=1;//start day
		$state = 'none';
		$dayAttr = 'none';
		$tsLast = strtotime("-1 month", strtotime($this->_startDate));
		$lastMonthTotal = cal_days_in_month(CAL_GREGORIAN, date('m', $tsLast), date('Y', $tsLast));
		//echo $lastMonthTotal;die();
		/* Determine if it is the current month, past or future */
		if($cYear < $this->_y || ($cYear==$this->_y && $cMonth < $this->_m)){
			/* We are in the past */	
			$state = "future";
			$dayAttr = 'active';
		}else if($cYear==$this->_y && $cMonth == $this->_m){/* This is the current month*/
			$state = "current";
			$dayAttr = 'none';
		}else if($cYear > $this->_y || ($cYear==$this->_y && $cMonth > $this->_m)){
			/* We are in the future */
			$state = "past";
			$dayAttr = 'inactive';
		}

		
		//echo $this->_totalDays;
		for($i=1; $iDay <= $this->_totalDays; $i++){

			if(($i-1)<$this->_startDay){
				$html .='<td class="days">';
	                        $html .='<div class="day previous">'.($lastMonthTotal-$this->_startDay+$i).'</div></td>';
			}else{
				if($state=='current'){
					if($iDay<$cDay){
						$dayAttr = 'inactive';
					}elseif($iDay==$cDay){
						$dayAttr = 'active today';
					}else{
						$dayAttr = 'active';
					}
				}
				
				$html .='<td class="days '.$dayAttr.'">';
	                        $html .='<div class="day">'.$iDay.'</div></td>';
				$iDay++;
			}
						
			//$html .='<td class="days active">'; 
			//$html .='<div class="day">'.$i.'</div></td>';
			
			if($i%7==0&&(($this->_totalDays+$this->_startDay-$i)>=0)){
				$html .= '</tr><tr class="weeks">';
			}

			if($iDay>$this->_totalDays){
				$stop = date('w',mktime(0, 0, 0, $this->_m, $this->_totalDays, $this->_y));
				for($z=1;$z<=(6-$stop);$z++){
					$html .= '<td class="days next"><div>'.$z.'</div></td> ';
				}
				$html .= '</tr>';
			}
		}
		$html .= "</tbody></table>";
		
		return $html;
	}
	
}

?>
